//
//  Utilities.m
//  eticket
//
//  Created by AnhQuocNguyen on 4/8/13.
//  Copyright (c) 2013 SoinMedia. All rights reserved.
//

#import "Utilities.h"

@implementation Utilities


////////////////////////////////////////////////////////////////////////////////////////////////
/* Validation */
#pragma mark - trimString
+ (NSString *)trimString:(NSString *)str{
    
    return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}


#pragma mark - isEmptyString
+ (BOOL)isEmptyString:(NSString *)str{
    if ([[self trimString:str] isEqualToString:@""] || [str length] == 0) {
        return YES;
    }
    return NO;
}


#pragma mark -
+ (BOOL)isValidEmail:(NSString *)str{
    BOOL stricterFilter = YES;
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:str];
}


#pragma mark - isPasswordRight
+ (BOOL)isPasswordRight:(NSString *)str{
    
    return NO;
}


#pragma mark - haveEmoji
+ (BOOL)haveEmoji:(NSString *)str{
    __block NSInteger count= 0;
    [str enumerateSubstringsInRange: NSMakeRange(0, [str length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
         
         const unichar hs = [substring characterAtIndex: 0];
         if (0xd800 <= hs && hs <= 0xdbff) {
             const unichar ls = [substring characterAtIndex: 1];
             const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
             if ((0x1d000 <= uc && uc <= 0x1f77f)) {
                 count++;
             }
         }else {
             if (0x2100 <= hs && hs <= 0x26ff) {
                 count++;
             }
         }
         
     }];
    return count > 0 ? YES : NO;
}


#pragma mark - removeEmoji
+ (NSString*)removeEmoji:(NSString *)str {
    __block NSMutableString* temp = [NSMutableString string];
    
    [str enumerateSubstringsInRange: NSMakeRange(0, [str length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
         
         const unichar hs = [substring characterAtIndex: 0];
         
         // surrogate pair
         if (0xd800 <= hs && hs <= 0xdbff) {
             const unichar ls = [substring characterAtIndex: 1];
             const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
             
             [temp appendString: (0x1d000 <= uc && uc <= 0x1f77f)? @"": substring]; // U+1D000-1F77F
             
             // non surrogate
         } else {
             [temp appendString: (0x2100 <= hs && hs <= 0x26ff)? @"": substring]; // U+2100-26FF
         }
     }];
    
    return temp;
}


#pragma mark - isDeviceFourInches
+ (BOOL)isDeviceFourInches{
    if ([UIScreen mainScreen].bounds.size.height == 568) {
        return YES;
    }
    return NO;
}


#pragma mark - stringByStrippingHTML
+ (NSString *)stringByStrippingHTML:(NSString *)htmlString {
    NSRange r;
    while ((r = [htmlString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        htmlString = [htmlString stringByReplacingCharactersInRange:r withString:@""];
    return htmlString;
}

////////////////////////////////////////////////////////////////////////////////////////////////
//Resize image
#pragma mark - ResizeForImage
+ (CGSize)getNewSizeForImage:(UIImage *)Original withMaxSize:(int)maxSize{
    CGSize newSize = CGSizeZero;
    CGSize currSize = Original.size;
    if (currSize.width > currSize.height) {
        if (currSize.width < maxSize) {
            return currSize;
        }else{
            newSize.width = maxSize;
            newSize.height = currSize.height/currSize.width *maxSize;
        }
    }else{
        if (currSize.height < maxSize) {
            return currSize;
        }else{
            newSize.width = currSize.width/currSize.height *maxSize;
            newSize.height = maxSize;
        }
    }
    return newSize;
}
+ (UIImage *)resizeImage:(UIImage *)image withSize:(CGSize)newSize
{
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


////////////////////////////////////////////////////////////////////////////////////////////////
//Check Network
+ (BOOL)isNetworkConnected
{
    Reachability *curReach = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [curReach currentReachabilityStatus];
    return (networkStatus != NotReachable);
}

+ (BOOL)isHostReachable{
    Reachability* hostReachable = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus networkStatus = [hostReachable currentReachabilityStatus];
    if (networkStatus!= NotReachable) {
        return YES;
    }
    return NO;
}


+ (BOOL)isConnectionAvailable
{
    return ([self isNetworkConnected] && [self isHostReachable]);
}


@end
